package com.situ.web.dao.impl;

import com.situ.web.dao.ITeacherDao;
import com.situ.web.pojo.Teacher;
import com.situ.web.pojo.query.TeacherQuery;
import com.situ.web.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeacherDaoImpl implements ITeacherDao {
    @Override
    public List<Teacher> selectByPage(TeacherQuery teacherQuery) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Teacher> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,age,gender,subject FROM teacher WHERE 1=1";
            //构造一个List存所有的条件
            List<Object> queryList = new ArrayList<>();
            String queryName = teacherQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " AND name LIKE ?";
                queryList.add("%" + queryName + "%");
            }
            String queryAge = teacherQuery.getAge();
            if (queryAge != null && !"".equals(queryAge)) {
                sql += " AND age=?";
                queryList.add(queryAge);
            }
            String queryGender = teacherQuery.getGender();
            if (queryGender != null && !"".equals(queryGender)) {
                sql += " AND gender=?";
                queryList.add(queryGender);
            }
            String querySubject = teacherQuery.getSubject();
            if (querySubject != null && !"".equals(querySubject)) {
                sql += " AND subject=?";
                queryList.add(querySubject);
            }
            sql += " LIMIT ?,?";
            int offset = (teacherQuery.getPage() - 1) * teacherQuery.getLimit();
            queryList.add(offset);
            queryList.add(teacherQuery.getLimit());
            statement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                statement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，没有返回false，如果有返回true，并且指向下一行
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                String subject = resultSet.getString("subject");
                Teacher teacher = new Teacher(id, name, age, gender, subject);
                list.add(teacher);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return list;
    }

    @Override
    public Integer selectTotalCount(TeacherQuery teacherQuery) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int totalCount = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT count(*) FROM teacher WHERE 1=1";
            //构造一个List存所有的条件
            List<Object> queryList = new ArrayList<>();
            String queryName = teacherQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " AND name LIKE ?";
                queryList.add("%" + queryName + "%");
            }
            String queryAge = teacherQuery.getAge();
            if (queryAge != null && !"".equals(queryAge)) {
                sql += " AND age=?";
                queryList.add(queryAge);
            }
            String queryGender = teacherQuery.getGender();
            if (queryGender != null && !"".equals(queryGender)) {
                sql += " AND gender=?";
                queryList.add(queryGender);
            }
            String querySubject = teacherQuery.getSubject();
            if (querySubject != null && !"".equals(querySubject)) {
                sql += " AND subject=?";
                queryList.add(querySubject);
            }
            statement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                statement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，没有返回false，如果有返回true，并且指向下一行
                totalCount = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }

        return totalCount;
    }

    @Override
    public void deleteById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "DELETE FROM teacher WHERE id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    @Override
    public void add(Teacher teacher) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "INSERT INTO teacher(name, age,gender,subject) VALUES(?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, teacher.getName());
            statement.setInt(2, teacher.getAge());
            statement.setString(3, teacher.getGender());
            statement.setString(4, teacher.getSubject());
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    @Override
    public Teacher selectById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Teacher teacher = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,age,gender,subject FROM teacher WHERE id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，没有返回false，如果有返回true，并且指向下一行
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                String subject = resultSet.getString("subject");
                teacher = new Teacher(id,name,age,gender,subject);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }

        return teacher;
    }

    @Override
    public void update(Teacher teacher) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "UPDATE teacher SET name=?,age=?,gender=?,subject=? WHERE id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, teacher.getName());
            statement.setInt(2, teacher.getAge());
            statement.setString(3, teacher.getGender());
            statement.setString(4, teacher.getSubject());
            statement.setInt(5, teacher.getId());
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }
}
